//
//  SurveyItem.m
//  Surveyer
//
//  Created by Qikun Guo on 13-8-16.
//  Copyright (c) 2013年 Tim. All rights reserved.
//

#import "SurveyItem.h"
#import "Result.h"
#import "Survey.h"


@implementation SurveyItem

@dynamic title;
@dynamic type;
@dynamic value1;
@dynamic value2;
@dynamic value3;
@dynamic value4;
@dynamic owner;
@dynamic result;

- (void)setValue:(id)value forKey:(NSString *)key
{
    if ([key hasPrefix:@"xvalue"])
    {
        //special ways to handle
    }
    else
        [super setValue:value forKey:key];
}

//hotfix of apple's bug
//remove these code if next ver. fix it officially!
- (void)addResultObject:(Result *)value
{
    NSMutableOrderedSet *temp = [NSMutableOrderedSet orderedSetWithOrderedSet:self.result];
    [temp addObject:value];
    self.result = temp;
}

- (NSString *)textOutput
{
    NSString *output = @"";
    int count = self.result.count;
    if ([self.type isEqualToString:@"Binary"])
    {
        int first=0;
        for (Result *r in self.result)
        {
            if ([r.text isEqualToString:@"0"])
                first++;
        }
        output = [output stringByAppendingFormat:NSLocalizedString(@"%d People chose option 1: %@\n%d People chose option 2: %@", @"Binary export"), first, self.value1, (count-first), self.value2];
    }
    else if ([self.type isEqualToString:@"Text Field"])
    {
        output = [output stringByAppendingFormat:NSLocalizedString(@"People's responses are as follows:", @"export")];
        for (Result *r in self.result)
        {
            output = [output stringByAppendingFormat:@"\n%@", r.text];
        }
    }
    
    else if ([self.type isEqualToString:@"Slider"])
    {
        output = [output stringByAppendingFormat:NSLocalizedString(@"People's responses are as follows:", @"export")];
        output = [output stringByAppendingString:@"\n"];
        double avg=0;
        for (Result *r in self.result)
        {
            output = [output stringByAppendingFormat:@"%@ ", r.text];
            avg += r.text.doubleValue;
        }
        output = [output stringByAppendingFormat:@"\nThe average is %f", avg/self.result.count];
    }
    else if ([self.type isEqualToString:@"Picker"])
    {
        int first, second, third;
        first = second = third =0;
        for (Result *r in self.result)
        {
            if ([r.text isEqualToString:@"0"])
                first++;
            else if ([r.text isEqualToString:@"1"])
                second++;
            else if ([r.text isEqualToString:@"2"])
                third++;
        }
        output = [output stringByAppendingFormat:NSLocalizedString(@"%d People chose option 1: %@\n%d People chose option 2: %@\n%d People chose option 3: %@\n%d People chose option 4: %@", @"Picker export"), first, self.value1, second, self.value2, third, self.value3, (count-first-second-third), self.value4];
    }
    else if ([self.type isEqualToString:@"Text Only"])
    {
        return NSLocalizedString(@"This is only a hint so there are no responses.", @"text only export");
    }
    else if ([self.type isEqualToString:@"Date Picker"])
    {
        //nil
    }
    else if ([self.type isEqualToString:@"Multi TF"])
    {
        //nil
    }
    return output;
}
@end
